<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>boost/python/implicit.hpp</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Boost.Python Reference Manual">
<link rel="up" href="../to_from_python_type_conversion.html" title="Chapter 5. To/From Python Type Conversion">
<link rel="prev" href="../to_from_python_type_conversion.html" title="Chapter 5. To/From Python Type Conversion">
<link rel="next" href="boost_python_lvalue_from_pytype_.html" title="boost/python/lvalue_from_pytype.hpp">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../../images/boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../to_from_python_type_conversion.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../to_from_python_type_conversion.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_lvalue_from_pytype_.html"><img src="../../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="to_from_python_type_conversion.boost_python_implicit_hpp"></a><a class="link" href="boost_python_implicit_hpp.html" title="boost/python/implicit.hpp">boost/python/implicit.hpp</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.function_template_implicit_conve">Function
        template <code class="computeroutput"><span class="identifier">implicit_convertible</span></code></a></span></dt>
<dt><span class="section"><a href="boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.example">Example</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_implicit_hpp.introduction"></a><a class="link" href="boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.introduction" title="Introduction">Introduction</a>
</h3></div></div></div>
<p>
          <code class="computeroutput"><span class="identifier">implicitly_convertible</span></code>
          allows Boost.Python to implicitly take advantage of a C++ implicit or explicit
          conversion when matching Python objects to C++ argument types.
        </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_implicit_hpp.function_template_implicit_conve"></a><a class="link" href="boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.function_template_implicit_conve" title="Function template implicit_convertible">Function
        template <code class="computeroutput"><span class="identifier">implicit_convertible</span></code></a>
</h3></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Source</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Target</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">implicitly_convertible</span><span class="special">();</span>
</pre>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Parameter
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody>
<tr>
<td>
                  <p>
                    Source
                  </p>
                </td>
<td>
                  <p>
                    The source type of the implicit conversion
                  </p>
                </td>
</tr>
<tr>
<td>
                  <p>
                    Target
                  </p>
                </td>
<td>
                  <p>
                    The target type of the implicit conversion
                  </p>
                </td>
</tr>
</tbody>
</table></div>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">Requires</span></dt>
<dd><p>
                The declaration <code class="computeroutput"><span class="identifier">Target</span>
                <span class="identifier">t</span><span class="special">(</span><span class="identifier">s</span><span class="special">);</span></code>,
                where s is of type Source, is valid.
              </p></dd>
<dt><span class="term">Effects</span></dt>
<dd><p>
                registers an rvalue <code class="computeroutput"><span class="identifier">from_python</span></code>
                converter to Target which can succeed for any <code class="computeroutput"><span class="identifier">PyObject</span><span class="special">*</span> <span class="identifier">p</span></code>
                iff there exists any registered converter which can produce Source
                rvalues
              </p></dd>
<dt><span class="term">Rationale</span></dt>
<dd><p>
                C++ users expect to be able to take advantage of the same sort of
                interoperability in Python as they do in C++.
              </p></dd>
</dl>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="to_from_python_type_conversion.boost_python_implicit_hpp.example"></a><a class="link" href="boost_python_implicit_hpp.html#to_from_python_type_conversion.boost_python_implicit_hpp.example" title="Example">Example</a>
</h3></div></div></div>
<p>
          In C++:
        </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="keyword">class</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">implicit</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">python</span><span class="special">/</span><span class="identifier">module</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">;</span>

<span class="keyword">struct</span> <span class="identifier">X</span>
<span class="special">{</span>
    <span class="identifier">X</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">v</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span>
    <span class="keyword">operator</span> <span class="keyword">int</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">v</span><span class="special">;</span> <span class="special">}</span>
    <span class="keyword">int</span> <span class="identifier">v</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">int</span> <span class="identifier">x_value</span><span class="special">(</span><span class="identifier">X</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span><span class="special">)</span>
<span class="special">{</span>
    <span class="keyword">return</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">v</span><span class="special">;</span>
<span class="special">}</span>

<span class="identifier">X</span> <span class="identifier">make_x</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">X</span><span class="special">(</span><span class="identifier">n</span><span class="special">);</span> <span class="special">}</span>

<span class="identifier">BOOST_PYTHON_MODULE</span><span class="special">(</span><span class="identifier">implicit_ext</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"x_value"</span><span class="special">,</span> <span class="identifier">x_value</span><span class="special">);</span>
    <span class="identifier">def</span><span class="special">(</span><span class="string">"make_x"</span><span class="special">,</span> <span class="identifier">make_x</span><span class="special">);</span>

    <span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;(</span><span class="string">"X"</span><span class="special">,</span>
        <span class="identifier">init</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;())</span>
        <span class="special">;</span>

    <span class="identifier">implicitly_convertible</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">,</span><span class="keyword">int</span><span class="special">&gt;();</span>
    <span class="identifier">implicitly_convertible</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span><span class="identifier">X</span><span class="special">&gt;();</span>
<span class="special">}</span>
</pre>
<p>
          In Python:
        </p>
<pre class="programlisting"><span class="special">&gt;&gt;&gt;</span> <span class="identifier">from</span> <span class="identifier">implicit_ext</span> <span class="identifier">import</span> <span class="special">*</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">x_value</span><span class="special">(</span><span class="identifier">X</span><span class="special">(</span><span class="number">42</span><span class="special">))</span>
<span class="number">42</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">x_value</span><span class="special">(</span><span class="number">42</span><span class="special">)</span>
<span class="number">42</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">x</span> <span class="special">=</span> <span class="identifier">make_x</span><span class="special">(</span><span class="identifier">X</span><span class="special">(</span><span class="number">42</span><span class="special">))</span>
<span class="special">&gt;&gt;&gt;</span> <span class="identifier">x_value</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span>
<span class="number">42</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2005, 2015 David Abrahams, Stefan Seefeld<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../to_from_python_type_conversion.html"><img src="../../images/prev.png" alt="Prev"></a><a accesskey="u" href="../to_from_python_type_conversion.html"><img src="../../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="boost_python_lvalue_from_pytype_.html"><img src="../../images/next.png" alt="Next"></a>
</div>
</body>
</html>
